<?php include('../variables/variables.php'); ?>

<?php

$con = mysql_connect($dbserver, $dbuser, $dbpass);
if ($con)
{
	mysql_select_db($db, $con);
	if (isset($_POST['inventory_id']))
	{
		$q = fquery($is_checked_out, array($_POST['inventory_id']));
		$result = mysql_query($q);
		if (mysql_num_rows($result) < 1)
		{
			mysql_close($con);
			header("location: /check_in.php?err=The book is already checked in");
		}
		else
		{
			session_start();
			$q1 = fquery($checkin_date, array($_POST['inventory_id']));
			$result1 = mysql_fetch_array(mysql_query($q1));
			$q = fquery($checkin_history, array($_SESSION['id'], $_POST['inventory_id'], $result1[0]));
			$result = mysql_query($q);
			if ($result)
			{
				$q = fquery($checkin_inventory, array($_POST['inventory_id']));
				$result = mysql_query($q);
				mysql_close($con);
				if ($result)
				{
					header("location: /check_in.php?msg=Book checked in successfully");
				}
				else
				{
					header("location: /check_in.php?err=Failed to checkin the book");
				}
			}
			else
			{
				mysql_close($con);
				header("location: /check_in.php?err=Failed to checkin the book");
			}
		}
	}
	else
	{
		mysql_close($con);
		header("location: /check_in.php?err=Please fill in all fields");
	}
}

?>
